package com.dLibs.utils
{
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class dragIt extends MovieClip
	{
		private var _mc					:MovieClip = new MovieClip();
		private var _mk					:MovieClip = new MovieClip();
		private var zoomin				:MovieClip;
		private var zoomout				:MovieClip;
		private var _mouseOut			:Boolean;
		private var xScaleOld			:Number;
		private var yScaleOld			:Number;
		private var iScale				:Number;
		private var newWidth			:Number;
		private var newHeight			:Number;
		private var so					:scaleObject;
		
		public function dragIt()
		{
			// Dont use it XD
		}
		
		public function drag(mc:MovieClip, mk:MovieClip, resized:Boolean = false):void
		{
			_mc = mc;
			_mk = mk;
			// @
			_mc.x = _mk.x;
			_mc.y = _mk.y;
			// @ 
			_mc.mask = _mk; // set Image masked
			_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragMc);
			_mc.addEventListener(MouseEvent.MOUSE_UP, unDragMc);
			// Change the scale ?
			if ( resized ) so = new scaleObject(_mc, { width: _mk.width, height: _mk.height });
		}
		
		/** Init and Stop the Dragger */
		
		private function dragMc(m:MouseEvent):void
		{
			_mouseOut = false;
			
			_mc.startDrag();
			//_mc.addEventListener(Event.ENTER_FRAME, verifyArraste);
			trace("StartDrag");
		}
		
		private function unDragMc(m:MouseEvent):void
		{
			_mc.stopDrag();
			_mc.removeEventListener(Event.ENTER_FRAME, verifyArraste);
			if ( !_mouseOut ) verifyArraste();
			trace("DragOut");
		}
		
		/** Adding zooming option */
		
		public function addZoom(_zoomin:MovieClip, _zoomout:MovieClip, scaleQuant:Number = 0.1):void
		{
			// iScale is the scale value
			iScale = scaleQuant;
			// Set the functions to zoom in and out
			zoomin = _zoomin;
			zoomout = _zoomout;
			zoomin.addEventListener(MouseEvent.CLICK, moreZoom);
			zoomout.addEventListener(MouseEvent.CLICK, lessZoom);
		}
		
		private function moreZoom(m:MouseEvent):void
		{
			zoomout.addEventListener(MouseEvent.CLICK, lessZoom);
			// record the actual scale x and y
			xScaleOld = _mc.scaleX;
			yScaleOld = _mc.scaleY;
			// Set the new scale
			TweenLite.to(_mc, 0.5, { scaleX : (xScaleOld + iScale), scaleY : (yScaleOld + iScale), onUpdate : verifyDrag, onUpdateParams : [null], onComplete : verifyDrag });
		}
		
		private function lessZoom(m:MouseEvent):void
		{
			// record the actual scale x and y
			xScaleOld = _mc.scaleX;
			yScaleOld = _mc.scaleY;
			// get the width and height
			newWidth = _mc.width;
			newHeight = _mc.height;
			// Set the new scale
			// TweenLite.to(_mc, 0.5, { scaleX : (xScaleOld - iScale), scaleY : (yScaleOld - iScale), onUpdate : verifyDrag, onUpdateParams : [null] });
			TweenLite.to(_mc, 0.5, { scaleX : (xScaleOld - iScale), scaleY : (yScaleOld - iScale), onUpdate : verifyBoth });
		}
		
		/** Move Objects ******************************************/
		
		public function addMovement(up:MovieClip, right:MovieClip, down:MovieClip, left:MovieClip):void
		{
			up.addEventListener(MouseEvent.CLICK, MoveUp);
			right.addEventListener(MouseEvent.CLICK, MoveRight);
			down.addEventListener(MouseEvent.CLICK, MoveDown);
			left.addEventListener(MouseEvent.CLICK, MoveLeft);
		}
		
		private function MoveUp(e:MouseEvent):void
		{
			TweenLite.to(_mc, 0.5, { y : _mc.y - 50, onUpdate : verifyArraste });
		}
		
		private function MoveRight(e:MouseEvent):void
		{
			TweenLite.to(_mc, 0.5, { x : _mc.x + 50, onUpdate : verifyArraste });
		}
		
		private function MoveDown(e:MouseEvent):void
		{
			TweenLite.to(_mc, 0.5, { y : _mc.y + 50, onUpdate : verifyArraste });
		}
		
		private function MoveLeft(e:MouseEvent):void
		{
			TweenLite.to(_mc, 0.5, { x : _mc.x - 50, onUpdate : verifyArraste });
		}
		
		/** Verify the limits of the dragger ***********************/
		
		private function verifyBoth():void
		{
			verifyDrag();
			verifyArraste();
		}
		
		private function verifyArraste(e:Event = null):void
		{
			// If exit the mask space
			if ( _mc.stage.mouseX > _mk.x + _mk.width || _mc.stage.mouseX < _mk.x || _mc.stage.mouseY < _mk.y || _mc.stage.mouseY > _mk.y + _mk.height ) 
			{
				_mouseOut = true;				
				_mc.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
			}
			
			if ( _mc.x > _mk.x ) TweenLite.to(_mc, 0.5, { x : _mk.x });
			if ( _mc.x < (( _mk.x + _mk.width) - _mc.width ) ) TweenLite.to(_mc, 0.5, { x : (( _mk.x + _mk.width) - _mc.width ) });
			if ( _mc.y > _mk.y ) TweenLite.to(_mc, 0.5, { y : _mk.y });
			if ( _mc.y < (( _mk.y + _mk.height ) - _mc.height ) ) TweenLite.to(_mc, 0.5, { y : (( _mk.y + _mk.height ) - _mc.height ) });
		}
		
		private function verifyDrag(e:Event = null):void
		{
			// verify content width and height
			if ( _mc.width < _mk.width || _mc.height < _mk.height ) 
			{
				zoomout.removeEventListener(MouseEvent.CLICK, lessZoom);
				so = new scaleObject(_mc, { width: _mk.width, height: _mk.height });
				_mc.x = _mk.x;
				_mc.y = _mk.y;
			}
			
		} // End Functions
		
	} // End Class
	
} // End Package